package com.hxd.otc.mvp.home;

import android.support.annotation.NonNull;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.common.enums.TakeOrderStatusEnum;
import com.hxd.otc.main.bean.TabBottomBean;
import com.hxd.otc.module.my.bean.AppInfoBean;
import com.hxd.otc.module.my.bean.BaseListBean;
import com.hxd.otc.module.my.bean.NoticeBean;
import com.hxd.otc.module.my.bean.TakeOrderStatusBean;
import com.hxd.otc.module.my.model.OtcAdvertModel;
import com.hxd.otc.module.my.model.UserModel;
import com.hxd.otc.module.order.bean.RedPointBean;
import com.hxd.otc.module.order.model.AllOrderModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.mvp.contract.MainContract;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.utils.LogUtils;
import com.hxd.otc.utils.ResourceUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作  者 :  胡庆岭
 * 时  间 :  2018/5/31 14:56
 * 描  述 :  ${ MainPresenter}
 */
public class MainPresenter extends BasePresenter<MainContract.IMainView> implements MainContract.Presenter {

    private final UserModel mUserModel;
    private final AllOrderModel allOrderModel;
    private final OtcAdvertModel mOtcAdvertModel;
    private TakeOrderStatusEnum mTakeOrderStatusEnum;


    public MainPresenter(MainContract.IMainView iMainView) {
        super(iMainView);
        mUserModel = UserModel.getInstance();
        allOrderModel = AllOrderModel.getInstance();
        mOtcAdvertModel = OtcAdvertModel.getInstance();
    }

    @Override
    public void redPointFlag() {
        allOrderModel.redPointFlag(new BaseObserver<RedPointBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull RedPointBean bean) {
                mView.getPointFlag(bean);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }

        }, mView.<BaseBean<RedPointBean>>bindAutoDispose());
    }

    @Override
    public void getUpdateVersionInfo() {
        mUserModel.getUpdateAppInfo(new BaseObserver<AppInfoBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull AppInfoBean appInfoBean) {
                mView.getUpdateVersionInfoSuccess(appInfoBean);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
            }
        }, mView.<BaseBean<AppInfoBean>>bindAutoDispose());
    }


    @Override
    public void getAfficheList(int currentPage, int showCount) {
        mUserModel.getNoticeList(currentPage, showCount, new BaseObserver<BaseListBean<NoticeBean>>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull BaseListBean<NoticeBean> noticeBean) {
                ArrayList<NoticeBean> list = noticeBean.getList();
                if (list == null || list.size() == 0) return;
                mView.getAfficheListSuccess(list);
                if (UserHelper.isLogin() && !list.get(0).isLook()) {//登录并且 没看过的第一条公告
                    lookList(list.get(0).getId());
                }
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                LogUtils.e(errMessage);
            }
        }, mView.<BaseBean<BaseListBean<NoticeBean>>>bindAutoDispose());
    }

    @Override
    public List<TabBottomBean> getBottomTabsData() {
        List<TabBottomBean> bottomTabs = new ArrayList<>();
        TabBottomBean homeBean = new TabBottomBean(ResourceUtil.getString(R.string.home_page), R.drawable.selector_main_radio_bottom_home);
        bottomTabs.add(homeBean);
        TabBottomBean dealBean = new TabBottomBean(ResourceUtil.getString(R.string.deal), R.drawable.selector_main_radio_bottom_fiat);
        bottomTabs.add(dealBean);
        TabBottomBean orderBean = new TabBottomBean(ResourceUtil.getString(R.string.order), R.drawable.selector_main_radio_bottom_order);
        bottomTabs.add(orderBean);
        TabBottomBean myBean = new TabBottomBean(ResourceUtil.getString(R.string.my_fragment), R.drawable.selector_main_radio_bottom_my);
        bottomTabs.add(myBean);
        return bottomTabs;
    }

    private void lookList(final String noticeId) {
        mUserModel.lookList(noticeId, new BaseObserver<String>() {

            @Override
            public void onNext(@NonNull String msg, @NonNull String s) {
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
            }
        }, mView.<BaseBean<String>>bindAutoDispose());
    }

    @Override
    public void queryTakeOrderStatus() {
        BaseObserver<TakeOrderStatusBean> baseObserver = new BaseObserver<TakeOrderStatusBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull TakeOrderStatusBean takeOrderStatusBean) {
                mTakeOrderStatusEnum = takeOrderStatusBean.getTakeOrderStatusEnum();
                mView.queryTakeOrderSuccess(mTakeOrderStatusEnum);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mTakeOrderStatusEnum = null;
                mView.queryTakeOrderFail(errMessage);
            }
        };
        baseObserver.setPostTokenMissEvent(false);
        mOtcAdvertModel.tradeStutsByUserId(baseObserver, mView.<BaseBean<TakeOrderStatusBean>>bindAutoDispose());
    }

    @Override
    public void updateTakeOrderStatus(final TakeOrderStatusEnum takeOrderStatusEnum) {
        if (UserHelper.isLogin() && UserHelper.isNonInductivePaySilver()) {
            mOtcAdvertModel.updateTradeStuts(takeOrderStatusEnum, new BaseObserver() {
                @Override
                public void onNext(@NonNull String msg, @NonNull Object o) {
                    mTakeOrderStatusEnum = takeOrderStatusEnum;
                    mView.updateTakeOrderStatusSuccess(takeOrderStatusEnum);
                }

                @Override
                public void onError(int errType, @NonNull String errMessage) {
                    mView.updateTakeOrderStatusFail(errMessage);
                }
            }, mView.<BaseBean>bindAutoDispose());
        }
    }

    @Override
    public TakeOrderStatusEnum getTakeOrderStatusEnum() {
        return mTakeOrderStatusEnum;
    }

    @Override
    public void getNosenseMessageNotice() {
        mUserModel.getNosenseMessageNotice(new BaseObserver<Integer>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull Integer numbers) {
                if (numbers != null)
                    mView.setNosenseMessageNotice(numbers);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {

            }

            @Override
            public void onNext(BaseBean<Integer> response) {
                if (response.getCode() == TOKEN_MISS || response.getCode() == TOKEN_NOT_MATCH) {
                    mView.clearNosenseRefresh();
                    onError(TOKEN_MISS, "");
                } else {
                    onNext(response.msg, response.getData());
                }

            }

        }, mView.<BaseBean<Integer>>bindAutoDispose());
    }
}
